Linux系统硬链接和软链接具体实例讲解(超详细) 您所在的位置:网站首页 linux 目录创建软连接 Linux系统硬链接和软链接具体实例讲解(超详细)

Linux系统硬链接和软链接具体实例讲解(超详细)

2024-07-11 09:33| 来源: 网络整理| 查看: 265

简介

在 Linux 中,元数据中的 inode 号(inode 是文件元数据的一部分但其并不包含文件名,inode 号即索引节点号)才是文件的唯一标识而非文件名。文件名仅是为了方便人们的记忆和使用,系统或程序通过 inode 号寻找正确的文件数据块。而有一种方法可以快速的寻找到数据元,那就是软硬链接 链接简单说实际上是一种文件共享的方式,是 POSIX 中的概念,主流文件系统都支持链接文件。

创建链接命令

软链接:ln -s 源文件 目标文件 硬链接:ln 源文件 目标文件

软链接文件的大小和创建时间和源文件不同。软链接文件只是维持了从软链接到源文件的指向关系(从jys.soft->jys可以看出),不是源文件的内容,大小不一样容易理解。 硬链接文件和源文件的大小和创建时间一样。硬链接文件的内容和源文件的内容一模一样,相当于copy了一份。 软链接 软链接(symbolic link) : 等同于 Windows 系统下的快捷方式。仅仅包括所含链接文件的路径名字。因此能链接目录,也能跨文件系统链接。但是,当删除原始文件后,链接文件也将失效。 硬链接(hard link) : 可以将它理解为一个 “指向原始文件 inode 的指针”,系统不为它分配独立的 inode 和 文件。所以,硬链接文件与原始文件其实是同一个文件,只是名字不同。我们每添加一个硬链接,该文件的 innode 连接数就会增加 1 ; 而且只有当该文件的 inode 连接数为 0 时,才算彻底被将它删除。因此即便删除原始文件,依然可以通过硬链接文件来访问。需要注意的是,我们不能跨分区对文件进行链接

实例详解

[root@localhost b]# echo “this is a test” >test.c [root@localhost b]# cat test.c this is a test [root@localhost b]# echo “this is a test2” >test2.c [root@localhost b]# cat test2.c this is a test2

创建一个软链接 [root@localhost b]# ln -s test.c soft [root@localhost b]# ls -li 总用量 8 15728644 lrwxrwxrwx 1 root root 6 7月 30 18:33 soft -> test.c 15728643 -rw-r--r-- 1 root root 16 7月 30 18:31 test2.c 15728642 -rw-r--r-- 1 root root 15 7月 30 18:30 test.c 创建一个硬链接 [root@localhost b]# ln test2.c hard [root@localhost b]# ls -li 总用量 12 15728643 -rw-r--r-- 2 root root 16 7月 30 18:31 hard 15728644 lrwxrwxrwx 1 root root 6 7月 30 18:33 soft -> test.c 15728643 -rw-r--r-- 2 root root 16 7月 30 18:31 test2.c 15728642 -rw-r--r-- 1 root root 15 7月 30 18:30 test.c

硬链接解析: 通过输出的文件属性可以知道创建的硬链接和文件本身的inode号是一样的都为15728643,这是因为硬链接和源文件同时是访问同一个地址空间,生成一个硬链接相当于copy一份该文件,如果删除源文件test.2.c,hard文件不会删除,文件的内容也是在的,具体如下:

[root@localhost b]# ls hard soft test2.c test.c [root@localhost b]# cat hard this is a test2 [root@localhost b]# cat test2.c this is a test2 [root@localhost b]# rm -rf test2.c [root@localhost b]# cat hard this is a test2 [root@localhost b]# ls -li 总用量 8 15728643 -rw-r--r-- 1 root root 16 7月 30 18:31 hard 15728644 lrwxrwxrwx 1 root root 6 7月 30 18:33 soft -> test.c 15728642 -rw-r--r-- 1 root root 15 7月 30 18:30 test.c

软链接解析: 生成的软链接相当于记录了源文件的地址,访问该软链接相当于直接访问该源文件,类似于windows的快捷方式,如果删除源文件,该软链接也会相应丢失源文件内容,访问出错 ,具体如下:

[root@localhost b]# ls -li 总用量 8 15728643 -rw-r--r-- 1 root root 16 7月 30 18:31 hard 15728644 lrwxrwxrwx 1 root root 6 7月 30 18:33 soft -> test.c 15728642 -rw-r--r-- 1 root root 15 7月 30 18:30 test.c [root@localhost b]# cat test.c this is a test [root@localhost b]# cat soft this is a test [root@localhost b]# rm -rf test.c [root@localhost b]# cat soft cat: soft: 没有那个文件或目录 [root@localhost b]# ls -li 总用量 4 15728643 -rw-r--r-- 1 root root 16 7月 30 18:31 hard 15728644 lrwxrwxrwx 1 root root 6 7月 30 18:33 soft -> test.c

若在软链接上重新写入内容 到soft中,则相当于生成了test.c文件,链接又重新生效,具体如下:

[root@localhost b]# ls -li 总用量 8 15728643 -rw-r--r-- 1 root root 16 7月 30 18:31 hard 15728644 lrwxrwxrwx 1 root root 6 7月 30 18:33 soft -> test.c 15728642 -rw-r--r-- 1 root root 14 7月 31 22:36 test.c [root@localhost b]# cat test.c this is test3 [root@localhost b]# cat soft this is test3


【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有